﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ShortcutValidator.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G. Vacirca.
// </author>
// <summary>
//   Defines the ShortcutsValidator type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Core
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    using Data;
    using Helpers;
    using log4net;

    /// <summary>
    /// Enables to validate the shortcuts data.
    /// </summary>
    public static class ShortcutValidator
    {
        #region "Private Members"

        /// <summary>
        /// Logger interface.
        /// </summary>
        private static readonly ILog _logger = LogManager.GetLogger(typeof(ShortcutValidator));

        #endregion

        #region "Public Methods"

        /// <summary>
        /// Does the validation process by checking the shortcuts path.
        /// </summary>
        /// <param name="shortcuts">The shortcuts. The Valid field will be updated.</param>
        /// <returns>
        /// An enumeration of the invalid shortcuts. Null if there are not invalid shortcuts.
        /// </returns>
        public static IEnumerable<ShortcutData> Validate(IEnumerable<ShortcutData> shortcuts)
        {
            try
            {
                var invalidShortcuts = DoValidation(shortcuts);
                return invalidShortcuts.Count().Equals(0) ? null : invalidShortcuts;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);               
                return null;
            }
        }

        /// <summary>
        /// Checks if the directory or file exists.
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        /// <returns>
        /// True: if the shortcuts path is valid; False: otherwise.
        /// </returns>
        public static bool ValidatePath(string fullPath)
        {
            if (File.Exists(fullPath) || Directory.Exists(fullPath) || fullPath.StartsWith("http") || fullPath.StartsWith("www") || fullPath.StartsWithIP())
            {
                return true;
            }

            return false;
        }

        #endregion

        #region "Private Methods"

        /// <summary>
        /// Does the validation process by checking the shortcuts path.
        /// </summary>
        /// <param name="shortcuts">The shortcuts. The Valid field will be updated.</param>
        /// <returns>
        /// An enumeration of the invalid shortcuts.
        /// </returns>
        private static IEnumerable<ShortcutData> DoValidation(IEnumerable<ShortcutData> shortcuts)
        {
            if (shortcuts.Count().Equals(0))
            {
                yield break;
            }

            foreach (var shortcutData in shortcuts)
            {
                shortcutData.Valid = ValidatePath(shortcutData.FullPath);
                if (!shortcutData.Valid)
                {                    
                    yield return shortcutData;
                }
            }
        }

        #endregion
    }
}
